package com.uchengwang.ui.cm_ui.ucity.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.ucity.adapter.UserAdapter;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/1/30.
 * 自己粉丝 关注 好友界面
 */

public class UsersActivity extends BaseWithTitleActivity{
    private long uid;//用户id
    private RecyclerView recycleView;
    private LinearLayoutManager layoutManager;
    private UserAdapter adapter;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private int page = 1;//当前页  每页请求多少数据
    private int totlaPagers ;//总页数【服务器返回】
    private int type;//数据类型 1粉丝 2关注 3好友

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        EventBus.getDefault().register(this);
        uid = getIntent().getLongExtra(BaseConfig.ID, 0);
        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);

        switch (type){
            case 1:
                setTitle("粉丝");
                if (uid ==UCommonUtil.getUtil().getId()){
                    //如果是自己的id 并且是获取粉丝  更新自己的新粉丝数量
                    HttpUserManager.getManager().getMyFans(this, true, new OnResultListener() {
                        @Override
                        public void onResult(int stutas, String msg, int total, String data) {
                        }
                    });
                }
                break;
            case 2:
                setTitle("关注");
                break;
            case 3:
                setTitle("好友");
//                if (uid == UCommonUtil.getUtil().getId()){
//                    setRightText("建群", new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Intent intent = new Intent(UsersActivity.this, FriendsActivity.class);
//                            intent.putExtra(BaseConfig.TYPE, 4);
//                            startActivity(intent);
//                        }
//                    });
//                }

                break;
        }

        initView();
    }

    private void initView() {
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);
        initData();
    }

    private void initData() {
        layoutManager = new LinearLayoutManager(this);
        adapter = new UserAdapter(this, type);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(layoutManager);
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getData(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 修改备注名成功 更新
     * */
    public void onEvent(UserBean userBean) {
        if (type ==3 && userBean != null){
            List<UserBean> l = adapter.getData();
            if (l != null && l.size() >0){
                for (UserBean bean : l){
                    if (bean.getId() == userBean.getId()){
                        bean.setRemarkName(userBean.getRemarkName());
                        adapter.notifyDataSetChanged();
                        break;
                    }
                }

            }
        }
    }

    /**
     * 获取自己的粉丝 关注 好友
     * 1粉丝 2关注 3好友
     * */
    private void getData(final boolean isRefresh){
        HttpMeManager.getPresenter().getFriends(this, uid, page, 10, type, 0, new HttpMeManager.UsersListener() {
            @Override
            public void result(int status, int count, List<UserBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                if (status != 0 ){
                    if (status ==1){
                        adapter.getData().clear();
                        adapter.notifyDataSetChanged();
                    }
                    return;
                }
                totlaPagers = count;
                if (list != null && list.size() > 0){
                    if (isRefresh){
                        adapter.setData(list);
                    }else {
                        adapter.getData().addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
